package com.qingzhuge.netty.server;

import com.qingzhuge.netty.common.autoconfigure.NettyServerProperties;
import com.qingzhuge.netty.common.handler.HeartBeatServerHandler;
import com.qingzhuge.netty.common.message.MessageDecoder;
import com.qingzhuge.netty.common.message.MessageEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;
/**
 * @author : zero.xiao
 * @description :
 * @date :2018/9/28 0028 下午 14:49
 * @since : 1.0.0
 * @modified :
 */
public class NettyServerInitializer extends  ChannelInitializer<SocketChannel>{

	private NettyServerProperties properties;

	public NettyServerInitializer(NettyServerProperties properties) {
		this.properties = properties;
	}

	@Override
	protected void initChannel(SocketChannel ch) {
		ChannelPipeline pipeline = ch.pipeline();
		//处理日志
		pipeline.addLast(new LoggingHandler(LogLevel.INFO));
		//处理心跳
		pipeline.addLast(new IdleStateHandler(4, 0, 0, TimeUnit.SECONDS));
		pipeline.addLast(new MessageEncoder());
		pipeline.addLast(new MessageDecoder());
		pipeline.addLast(new HeartBeatServerHandler(properties.getHeartbeat()));
	}
}
